package com.tianqu.tms.www.car.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAjaxAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.MarketCarGateInfo;
import com.tianqu.tms.www.car.actionform.CarImportExportMonitoringForm;

import net.sf.json.JSONObject;

public class CarImportExportMonitoringAjaxAction extends TmsAjaxAction {
	private static Logger logger = Logger.getLogger(CarImportExportMonitoringAjaxAction.class);
	private static HashMap<String, CarImportExportMonitoringForm> gatewayMap = new HashMap<String, CarImportExportMonitoringForm>();
	private MarketCarGateInfo gateInfo = new MarketCarGateInfo();
	@Override
	public void process(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws TmsException {
		String gateId1 = (String) uc.getParameter("gateId1");
		String gateId2 = (String) uc.getParameter("gateId2");

		CarImportExportMonitoringForm v1 = gatewayMap.get(gateId1);
		CarImportExportMonitoringForm v2 = gatewayMap.get(gateId2);
		
		Date date = new Date();//当前日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化对象
		JSONObject json = new JSONObject();
		
		if(StringUtil.isNotBlank(gateId1) && StringUtils.isNumeric(gateId1)){
			if(v1 ==null){
				List list = gateInfo.findGateInfoByGateId(Integer.valueOf(gateId1),sdf.format(date));
				if(list.size()>0){
					MarketCarGateInfo info = (MarketCarGateInfo) list.get(0);
					v1 = setCarGateInfo(info);
					gatewayMap.put(gateId1, v1);
				}
			}
			if (v1 != null) {
				JSONObject json1 = new JSONObject();
				json1.put("gateId1", gateId1);
				json1.put("insertTimes", v1.getInsertTimes());
				json1.put("licensePlate", v1.getLicensePlate());
				json1.put("tempLicensePlate", v1.getTempLicensePlate());
				json1.put("threadStatusInfo", v1.getThreadStatusInfo());
				json1.put("memberName", v1.getMemberName());
				json1.put("threadTypeInfo", v1.getThreadTypeInfo());
				if(StringUtil.isNotBlank(v1.getCarPhoto())) json1.put("carPhoto", v1.getCarPhoto());
				json1.put("checkFlag", v1.getCheckFlagStr());
				json.put("gate1", json1);
			}
		}
		
		if(StringUtil.isNotBlank(gateId2) && StringUtils.isNumeric(gateId2)){
			if(v2 == null){
				List list = gateInfo.findGateInfoByGateId(Integer.valueOf(gateId2),sdf.format(date));
				if(list.size()>0){
					MarketCarGateInfo info = (MarketCarGateInfo) list.get(0);
					v2 = setCarGateInfo(info);
					gatewayMap.put(gateId2, v2);
				}
			}
			if (v2 != null) {
				JSONObject json2 = new JSONObject();
				json2.put("gateId2", gateId2);
				json2.put("insertTimes", v2.getInsertTimes());
				json2.put("licensePlate", v2.getLicensePlate());
				json2.put("tempLicensePlate", v2.getTempLicensePlate());
				json2.put("threadStatusInfo", v2.getThreadStatusInfo());
				json2.put("memberName", v2.getMemberName());
				json2.put("threadTypeInfo", v2.getThreadTypeInfo());
				if(StringUtil.isNotBlank(v2.getCarPhoto())) json2.put("carPhoto",  v2.getCarPhoto());
				json2.put("checkFlag", v2.getCheckFlagStr());
				json.put("gate2", json2);
			}
		}
		responseValues(response, json.toString(), TEXT);
	}

	public static void push(String carPhoto,String gateNo,String plateNum,String rfid,String carSerial,int threadStatus,int threadType,int checkFlag) throws TmsDaoException {
		gatewayMap.put(gateNo, setCarGateInfo(carPhoto,plateNum,rfid,carSerial,threadStatus,threadType,checkFlag));
	}

	private static CarImportExportMonitoringForm setCarGateInfo(MarketCarGateInfo info) throws TmsDaoException {
		return setCarGateInfo(info.getCarPhoto(),info.getLicensePlate(),info.getTempLicensePlate(),info.getSerial(),info.getThreadStatus(),info.getThreadType(),info.getCheckFlag());
	}
	
	private static CarImportExportMonitoringForm setCarGateInfo(String carPhoto,String plateNum,String tempLicensePlate,String carSerial,int threadStatus,int threadType,int checkFlag) throws TmsDaoException {
		CarImportExportMonitoringForm info = new CarImportExportMonitoringForm();
		Car car = new Car();
		Codes code = Codes.getInstance();
		SimpleDateFormat fm = new SimpleDateFormat("HH:mm");
		// 进出时间
		info.setInsertTimes(fm.format(new Date()));
		// 商户名称
		info.setMemberName(car.findShortNameBySerial(carSerial));
		// 易碎贴
//		if (!StringUtil.isNullOrBlank(tempLicensePlate) && tempLicensePlate.length()>8)  tempLicensePlate = tempLicensePlate.substring(tempLicensePlate.length() - 8, tempLicensePlate.length());
		info.setTempLicensePlate(tempLicensePlate);
		//车牌
		info.setLicensePlate(plateNum);
		// 进出状态
		String threadStatusInfo = code.getItemValueByItemIdAndItemCode(Codes.THREADSTATUS_TYPE, threadStatus);
		info.setThreadStatusInfo(threadStatusInfo);
		// 识别方式
		String threadTypeInfo = code.getItemValueByItemIdAndItemCode(Codes.THREADTYPE_TYPE,threadType);
		info.setThreadTypeInfo(threadTypeInfo);
		// 过滤方式
		if (checkFlag == 1) info.setCheckFlagStr("off");
		else  info.setCheckFlagStr("on");
		info.setCarPhoto(carPhoto);
		return info;
	}

}
